package org.wmq.shopmanager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wmq.commonutil.result.Result;
import org.wmq.shopmanager.service.ProductService;
import org.wmq.shopmodel.DTO.product.ProductQueryDTO;
import org.wmq.shopmodel.Entity.product.Product;

@RestController
@RequestMapping("/admin/product/product")
@RequiredArgsConstructor
public class ProductController {
    private final ProductService productService;

    @GetMapping("list")
    public Result<IPage<Product>> list(@ModelAttribute ProductQueryDTO productQueryDTO){
        return Result.success(this.productService.getPageItem(productQueryDTO));
    }

    @PostMapping("/save")
    public Result<Void> save(@RequestBody Product product){
        this.productService.saveProduct(product);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result<Product> getProductById(@PathVariable Long id){
        return Result.success(this.productService.getProductAllFieldById(id));
    }

    @PutMapping("/updateById")
    public Result<Void> updateProductById(@RequestBody Product product){
        this.productService.updateProductById(product);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteProductById(@PathVariable Long id){
        this.productService.removeProductById(id);
        return Result.success();
    }
}
